import HeadPrefab from "../../../component/HeadPrefab";
import { EventManager } from "../../../engine/EventManager";
import { Logger } from "../../../engine/Logger";
import JhaoScrollViewLayout from "../../../engine/ui/scrollview/JhaoScrollViewLayout";
import UIManager from "../../../engine/UIManager";
import ViewBase from "../../../engine/ViewBase";
import { JhaoProto } from "../../../lib/net/protobuf/jhaoproto";
import { ClubSocketClient } from "../../../net/ClubSocketClient";
import { SocketClient } from "../../../net/SocketClient";
import { ClubConfig } from "../../config/ClubConfig";
import { EventConfig } from "../../config/EventConfig";
import { ViewConfig } from "../../config/ViewConfig";
import { ClubModel } from "../../model/ClubModel";
import { UserModel } from "../../model/UserModel";
import { UIHelper } from "../../util/UIHelper";

const { ccclass, property } = cc._decorator;

@ccclass
export default class ViewClubList extends ViewBase {

    private btnCreateClub: cc.Node
    private btnJoinClub: cc.Node
    private layout: JhaoScrollViewLayout

    onEnable(): void {
        super.onEnable()
        EventManager.instance.on(EventConfig.Event_Update_Club_List, this.refresh, this)
        let btnBack: cc.Node = this.findChild(this.node, "btnBack")
        UIHelper.onAddEvent(btnBack, () => {
            this.closeView()
            UIManager.instance.showView(ViewConfig.viewConfig.ViewHall)
        })
        let controlNode: cc.Node = this.findChild(this.node, "controlNode")
        this.btnCreateClub = this.findChild(controlNode, "btnCreateClub")
        UIHelper.onAddEvent(this.btnCreateClub, () => {
            UIManager.instance.showView(ViewConfig.viewConfig.ViewClubCreate)
        })
        this.layout = this.getChild(this.node, "clubListContainer/view/content", JhaoScrollViewLayout)
        this.btnJoinClub = this.findChild(controlNode, "btnJoinClub")
        UIHelper.onAddEvent(this.btnJoinClub, () => {
            UIManager.instance.showView(ViewConfig.viewConfig.ViewClubJoin)
        })

        ClubSocketClient.req_club_list()
        this.refresh()
    }

    private refresh() {
        this.layout.total(UserModel.instance.clubList.length)
    }


    private refreshClubItem(item: cc.Node, layerNodeList: Array<cc.Node>, index: number) {
        let model: JhaoProto.IClubInfo = UserModel.instance.clubList[index]
        // Logger.log("refreshClubItem=", model, index, UserModel.instance)
        if (model) {
            let head: HeadPrefab = this.getChild(layerNodeList[0], "head", HeadPrefab)
            head.setHead(model.headimg)
            let txtId: cc.Label = this.getChild(layerNodeList[1], "txtId", cc.Label)
            txtId.string = "ID:" + model.clubid + ""
            let txtName: cc.Label = this.getChild(layerNodeList[1], "txtName", cc.Label)
            txtName.string = model.clubName
            let txtOwner: cc.Label = this.getChild(layerNodeList[1], "txtOwner", cc.Label)
            txtOwner.string = "圈主:" + model.nickName
            let txtMember: cc.Label = this.getChild(layerNodeList[1], "txtMember", cc.Label)
            txtMember.string = "成员:" + model.usercount + "人"
            let txtState:cc.Label =  this.getChild(layerNodeList[1], "txtState", cc.Label)
            if (model.clubState == ClubConfig.Club_State_Type.dayang) {
                txtState.string = "已打烊"
            } else {
                txtState.string = ""
            }
            UIHelper.onAddEvent(item, () => {
                Logger.log("refreshClubItem===2=", model)
                UserModel.instance.nowClub = model;
                UIManager.instance.showView(ViewConfig.viewConfig.ViewClubHall, () => {
                    UIManager.instance.closeSingleView(ViewConfig.viewConfig.ViewClubList)
                })
            })
        }
    }

}
